#include "resetpassworddialog.h"
#include "ui_resetpassworddialog.h"
#include <QCheckBox>
#include <QPushButton>

ResetPasswordDialog::ResetPasswordDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ResetPasswordDialog)
{
    ui->setupUi(this);

    //设置图标与标题
    this->setWindowTitle("WeChat");
    this->setWindowIcon(QIcon(":/images/icon.ico"));

    ui->pushButton->setEnabled(false);
    ui->reset_name->setPlaceholderText("请输入账号");
    ui->reset_password1->setPlaceholderText("请输入密码");
    ui->resetpassword2->setPlaceholderText("再次输入密码");
    ui->reset_email->setPlaceholderText("请输入您的邮箱");
    ui->reset_veri->setPlaceholderText("邮箱验证码");
    ui->reset_password1->setEchoMode(QLineEdit::Password);
    ui->resetpassword2->setEchoMode(QLineEdit::Password);

    //设置定时器为1秒，当验证码验证错误达到一定次数时启动
    timer= new QTimer();
    timer->setInterval(1000);
    connect(timer,&QTimer::timeout,this,&ResetPasswordDialog::slot_permit);

    // 创建 QCheckBox 控件（父类选择QLineEdit）,使密码框右侧出现眼睛符号
    QCheckBox *checkBox = new QCheckBox(ui->reset_password1);
    checkBox->setCursor(Qt::PointingHandCursor);
    // 设置样式表（图片为眼睛样式）
    checkBox->setStyleSheet("QCheckBox {spacing: 5px;border: none;background-color: transparent;}"
                            "QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/images/unvisible.png);}"
                            "QCheckBox::indicator:checked {image: url(:/images/visible.png);}");
    //创建信号和槽连接,控制密码是否明文显示
    connect(checkBox, &QCheckBox::stateChanged,this, &ResetPasswordDialog::slot_checkbox_state_changed);
    //设置控件的位置（需要根据QLineEdit的坐标进行调整）
    checkBox->setGeometry(ui->reset_password1->pos().x() + 250,ui->reset_password1->pos().y() + 7,20,20);

    // 使邮箱输入框右边显示发送验证码按钮
    verificatioon_code = new QPushButton(ui->reset_email);
    verificatioon_code->setText("发送验证码");
    verificatioon_code->setCursor(Qt::PointingHandCursor);
    // 设置样式表（图片为眼睛样式）
    verificatioon_code->setStyleSheet("QPushButton {border: none;background-color: transparent;}");
    //创建信号和槽连接,触发滑动验证窗口
    connect(verificatioon_code, &QPushButton::clicked,this, &ResetPasswordDialog::slot_checkbox_clicked);
    //设置控件的位置（需要根据QLineEdit的坐标进行调整）
    verificatioon_code->setGeometry(ui->reset_email->pos().x() + 210,ui->reset_email->pos().y() + 7,65,20);
}

ResetPasswordDialog::~ResetPasswordDialog()
{
    delete ui;
}

void ResetPasswordDialog::slot_checkbox_state_changed(int state)
{
    if(state){
        ui->reset_password1->setEchoMode(QLineEdit::Normal);
        ui->resetpassword2->setEchoMode(QLineEdit::Normal);
    }else{
        ui->reset_password1->setEchoMode(QLineEdit::Password);
        ui->resetpassword2->setEchoMode(QLineEdit::Password);
    }
}

void ResetPasswordDialog::slot_checkbox_clicked()
{
    open_slidver_count++;
    //当验证码验证错误达到两次，触发等待30秒后才能再次验证
    if(open_slidver_count>2){
        verificatioon_code->setEnabled(false);
        timer->start();
        return;
    }
    _veri_dlg = new SlidingVerificationDialog(this);
    _veri_dlg->setAttribute(Qt::WA_DeleteOnClose,true);
    _veri_dlg->show();
}

void ResetPasswordDialog::slot_permit()
{
    if(num_time<=0){
        verificatioon_code->setEnabled(true);
        verificatioon_code->setText("发送验证码");
        timer->stop();
        num_time=30;
        return;
    }
    num_time--;
    QString str= QString::asprintf("剩余%d秒",num_time);
    verificatioon_code->setText(str);
}
